package com.firefoxking.mysite.controller.admin;

import com.firefoxking.mysite.dao.LogsMapper;
import com.firefoxking.mysite.dao.dto.StatisticsDto;
import com.firefoxking.mysite.entity.Comment;
import com.firefoxking.mysite.entity.Content;
import com.firefoxking.mysite.entity.Logs;
import com.firefoxking.mysite.service.log.LogService;
import com.firefoxking.mysite.service.site.SiteService;
import com.firefoxking.mysite.utils.Commons;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping(value = "/admin")
public class AdminIndexController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdminIndexController.class);

    @Autowired
    private Commons commons;

    @Autowired
    private SiteService siteService;

    @Autowired
    private LogService logService;


    @GetMapping(value = {"", "/index"})
    public String index(Model model) {
        LOGGER.info("Enter admin index method");
        List<Comment> comments = siteService.getComments(5);
        List<Content> contents = siteService.getNewArticles(5);
        StatisticsDto statistics = siteService.getStatistics();
        // 取最新的20条日志
        PageInfo<Logs> logs = logService.getLogs(1, 5);
        List<Logs> list = logs.getList();
        model.addAttribute("comments", comments);
        model.addAttribute("articles", contents);
        model.addAttribute("statistics", statistics);
        model.addAttribute("logs", list);
        LOGGER.info("Exit admin index method");
        model.addAttribute("commons", commons);
        return "admin/index";
    }

    @GetMapping(value = "/profile")
    public String profile(Model model) {
        model.addAttribute("commons", commons);
        return "admin/profile";
    }
}
